package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.enums.DictEnum;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.mapper.UserInfoMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName PatientServiceImpl
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/7/21 11:43
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/7/21 11:43
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    //远程调用字典服务
    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 根据用户id查询d对应下面的所有就诊人信息
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public List<Patient> findAllUserId(Long userId) {
        List<Patient> patients = baseMapper.selectList(new QueryWrapper<Patient>().eq("user_id", userId));
        //遍历集合将每个patient对象中参数进行封装,(获取字符串类型的证件类型名称，省市区名称，详细地址)
        patients.stream().forEach(item -> {
            this.packPatient(item);
        });

        return patients;
    }

    /**
     * 根据就诊人id获取详细信息
     * @param id
     * @return
     */
    @Override
    public Patient getParentId(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packPatient(patient);
        return patient;
    }

    //patient对象中参数进行封装
    private Patient packPatient(Patient patient) {
        //获取证件类型，根据code获取证件类型名称
        String certificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        //联系人证件类型
        String contactsCertificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        //封装参数
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
